home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Best of MacTutor - S…e Code for Volumes 1 to 5
/
The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin
/
Source Code
/
#49 (Oct 89)
/
C INIT π
/
example.c
< prev
next >
Wrap
C/C++ Source or Header
|
1989-06-25
|
3KB
|
185 lines
#include <SetUpA4.h>
#define nil 0l
#define ChangedResourceTrap 0xA9AA
#define AddResourceTrap 0xA9AB
Handle queryDITL; /* DITL for unknown dialog */
Handle resourceTypes; /* handle of all resource types to watch */
long oldChangedResource; /* address of original ChangedResource */
long oldAddResource; /* address of original AddResource */
Str255 trash; /* needed due to Apple bug ? */
pascal void NewChangedResource(Handle h);
pascal void NewAddResource(Handle h, ResType rType, int id, Str255 name);
Boolean userDialog();
Boolean inList(ResType type, Handle list);
Str255 *findFileName(int refNum);
void main(void);
pascal void NewChangedResource(h)
Handle h;
{
int id;
ResType type;
Str255 resName, num;
Boolean ok;
SetUpA4();
ok = true;
GetResInfo(h, &id, &type, &resName);
if (inList(type, resourceTypes) )
ok = userDialog("\pAttempt to Change Resource",
type, id, &resName, HomeResFile(h) );
if (ok)
CallPascal(h, oldChangedResource);
else
ResErr = resAttrErr;
RestoreA4();
}
pascal void NewAddResource(h, rType, id, name)
Handle h;
ResType rType;
int id;
Str255 name;
{
Boolean ok;
SetUpA4();
ok = true;
if (inList(rType, resourceTypes) )
ok = userDialog("\pAttempt to Add Resource",
rType, id, name, CurResFile() );
if (ok)
CallPascal(h, rType, id, name, oldAddResource);
else
ResErr = addResFailed;
RestoreA4();
}
Boolean userDialog(message, type, id, resName, file)
Str255 *message;
ResType type;
int id;
Str255 *resName;
int file;
{
GrafPtr oldPort;
Handle tempH;
DialogPtr d;
int i;
Rect r;
Str255 num;
GetPort(&oldPort);
tempH = queryDITL;
HandToHand(&tempH);
SetRect(&r, 90, 68, 444, 226);
d = NewDialog(nil, &r, nil, true, 1, -1, false, nil, tempH);
SetPort(d);
MoveTo(20,20);
DrawString(message);
MoveTo(20,40);
DrawString("\pResource Type: ");
DrawText(&type, 0, 4);
MoveTo(20,60);
DrawString("\pResource ID: ");
NumToString((long)id, num);
DrawString(num);
MoveTo(20,80);
DrawString("\pResource Name: ");
DrawString(resName);
MoveTo(20,100);
DrawString("\pFile Name: ");
DrawString( findFileName( file ) );
do {
ModalDialog(nil, &i);
} while ( i != 1 && i !=2 );
DisposDialog(d);
SetPort(oldPort);
return( i == 1);
}
Boolean inList(type, list)
ResType type;
Handle list;
{
int len;
ResType *resPtr;
len = GetHandleSize(list) >> 2;
resPtr = (ResType *)*list;
while ( len-- )
if (*resPtr++ == type)
return(true);
return(false);
}
Str255 *findFileName(refNum)
int refNum;
{
FCBPBRec p;
p.ioCompletion = 0;
p.ioRefNum = refNum;
p.ioFCBIndx = 0;
p.ioVRefNum = 0;
p.ioNamePtr = (StringPtr)trash;
PBGetFCBInfo(&p, false);
return((Str255 *)p.ioNamePtr);
}
/* This block is called once. It saves the pointer
to this code resource, and installs the patch. */
void main()
{
Handle myHandle;
Ptr myPtr;
SysEnvRec world;
Str255 *namePtr;
asm {
move.l A0, myPtr
}
RememberA0();
SetUpA4();
if(!Button()) {
myHandle = RecoverHandle(myPtr);
DetachResource(myHandle);
resourceTypes = GetResource('ResT', 256);
DetachResource(resourceTypes);
queryDITL = GetResource('DITL', 256);
DetachResource(queryDITL);
oldChangedResource = NGetTrapAddress(ChangedResourceTrap,ToolTrap);
NSetTrapAddress(NewChangedResource,ChangedResourceTrap,ToolTrap);
oldAddResource = NGetTrapAddress(AddResourceTrap,ToolTrap);
NSetTrapAddress(NewAddResource,AddResourceTrap,ToolTrap);
}
RestoreA4();
}